package cn.quickcode.monitor.ui.part;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.ResourceManager;
import org.jfree.experimental.chart.swt.ChartComposite;

import cn.quickcode.monitor.SMR;
import cn.quickcode.monitor.core.Task;
import cn.quickcode.monitor.core.TaskManager;
import cn.quickcode.monitor.model.Dbm;

/**
 * 监控面板、仪表盘、控制台
 * 
 * @author zhangaoxiang
 * @version $Revision$ - $Author$ at $Date$
 */
public class MCPViewPart extends ViewPart
{

	public static final String ID = "cn.quickcode.monitor.ui.part.MCPViewPart"; //$NON-NLS-1$

	public Action cfgAction;
	
	public Action runAction;
	
//	public ChartComposite cc=null;

	public MCPViewPart()
	{
	}

//	private JFreeChart createBarChart()
//	{
//		String title = "空调2002年市场占有率";
//		DefaultPieDataset piedata = new DefaultPieDataset();
//		piedata.setValue("联想", 27.3);
//		piedata.setValue("长城", 12.2);
//		piedata.setValue("海尔", 5.5);
//		piedata.setValue("美的", 17.1);
//		piedata.setValue("松下", 9.0);
//		piedata.setValue("科龙", 19.0);
//		JFreeChart chart = ChartFactory.createPieChart(title, piedata, true, true, true);
//		chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));
//		chart.addSubtitle(new TextTitle("2002财年分析", new Font("隶书", Font.ITALIC, 12)));
//		chart.setBackgroundPaint(Color.white);
//		PiePlot pie = (PiePlot) chart.getPlot();
//		pie.setBackgroundPaint(Color.white);
//		pie.setBackgroundAlpha(0.6f);
//		pie.setForegroundAlpha(0.90f);
//		return chart;
//	}
//	private JFreeChart createBarChart1()
//	{
//		String title = "空调2008年市场占有率";
//		DefaultPieDataset piedata = new DefaultPieDataset();
//		piedata.setValue("联想", 60.3);
//		piedata.setValue("长城", 30.2);
//		piedata.setValue("海尔", 80.5);
//		piedata.setValue("美的", 120.1);
//		piedata.setValue("松下", 18.0);
//		piedata.setValue("科龙", 19.0);
//		JFreeChart chart = ChartFactory.createPieChart(title, piedata, true, true, true);
//		chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));
//		chart.addSubtitle(new TextTitle("2008财年分析", new Font("隶书", Font.ITALIC, 12)));
//		chart.setBackgroundPaint(Color.white);
//		PiePlot pie = (PiePlot) chart.getPlot();
//		pie.setBackgroundPaint(Color.white);
//		pie.setBackgroundAlpha(0.6f);
//		pie.setForegroundAlpha(0.90f);
//		return chart;
//	}

	/**
	 * Create contents of the view part.
	 * 
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent)
	{
		Composite container = new Composite(parent, SWT.NONE);
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.marginHeight = 2;
		gl_container.marginWidth = 2;
		gl_container.verticalSpacing = 2;
		container.setLayout(gl_container);
//		JFreeChart chart = this.createBarChart();
//		cc = new ChartComposite(container, SWT.NONE, chart, true);
//		cc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		createActions();
		initializeToolBar();
	}

	/**
	 * Create the actions.
	 */
	private void createActions()
	{
		{
			this.cfgAction = new Action()
			{

				public void run()
				{
					MessageDialog.openInformation(getSite().getShell(), "配置", "配置");
//					MCPViewPart.this.cc.setChart(MCPViewPart.this.createBarChart1());
//					MCPViewPart.this.cc.forceRedraw();
				}
			};
			this.cfgAction.setText("配置");
			this.cfgAction.setToolTipText("监控参数配置");
			this.cfgAction.setImageDescriptor(SMR.BUTTON_ACTION_CFG);
		}
		{
			this.runAction = new Action()
			{

				public void run()
				{
					if(MCPViewPart.this.runAction.getImageDescriptor().equals(SMR.BUTTON_ACTION_STR))
					{
						MCPViewPart.this.runAction.setImageDescriptor(SMR.BUTTON_ACTION_STP);
						MCPViewPart.this.runAction.setText("停止");
						MCPViewPart.this.runAction.setToolTipText("停止监控");
					}
					else
					{
						MCPViewPart.this.runAction.setImageDescriptor(SMR.BUTTON_ACTION_STR);
						MCPViewPart.this.runAction.setText("开启");
						MCPViewPart.this.runAction.setToolTipText("开启监控");
					}
				}
			};
			this.runAction.setImageDescriptor(SMR.BUTTON_ACTION_STR);
			this.runAction.setText("开启");
			this.runAction.setToolTipText("开启监控");
		}
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar()
	{
		IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
		toolbarManager.add(this.runAction);
		toolbarManager.add(this.cfgAction);
	}

	@Override
	public void setFocus()
	{
		// Set the focus
	}
}
